package com.ququ.syb.flow.service;

import com.ququ.common.result.Result;
import com.ququ.common.result.ResultJson;
import com.ququ.syb.common.pojo.AccountBalanceFlow;
import com.ququ.syb.flow.dao.AccountBalanceFlowDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AccountBalanceFlowService {
    @Autowired
    private FlowSequenceService flowSequenceService;
    @Autowired
    private AccountBalanceFlowDao accountBalanceFlowDao;

    public Result<List<AccountBalanceFlow>> listAccountBalanceFlow(String accountid, String tradenum, Long amount, Integer flowtype,String remark){
        AccountBalanceFlow accountBalanceFlow = new AccountBalanceFlow();
        accountBalanceFlow.setAccountid(accountid);
        accountBalanceFlow.setTradenum(tradenum);
        accountBalanceFlow.setChangebalance(amount);
        accountBalanceFlow.setFlowtype(flowtype);
        accountBalanceFlow.setRemark(remark);
        List<AccountBalanceFlow> accountBalanceFlowList = accountBalanceFlowDao.getList(accountBalanceFlow);
        if (accountBalanceFlowList == null || accountBalanceFlowList.size() == 0)
            return new Result<>(-1,"查无数据");
        else
            return new Result<>(accountBalanceFlowList);
    }


    public Result<AccountBalanceFlow> saveAccountBalanceFlow(String accountid,String tradenum,Long currentbalance,Long amount,Long afterbalance,
                                                             Integer flowtype,String remark,String appcode,String paychannelid,Integer ordertype){
        AccountBalanceFlow accountBalanceFlow = new AccountBalanceFlow();
        accountBalanceFlow.setId(flowSequenceService.getNextId());
        accountBalanceFlow.setAccountid(accountid);
        accountBalanceFlow.setTradenum(tradenum);
        accountBalanceFlow.setChangebalance(amount);
        accountBalanceFlow.setFlowtype(flowtype);
        accountBalanceFlow.setCurrentbalance(currentbalance);
        accountBalanceFlow.setAfterbalance(afterbalance);
        accountBalanceFlow.setRemark(remark);
        accountBalanceFlow.setAppcode(appcode);
        accountBalanceFlow.setPaychanelid(paychannelid);
        accountBalanceFlow.setSourcetype(ordertype);
        accountBalanceFlow.setCreatetime(new Date());
        accountBalanceFlowDao.save(accountBalanceFlow);
        return new Result<>(accountBalanceFlow);
    }

    /**
     * 删除指定订单号的所有流水记录，用于提现失败订单改为成功
     * @param tradenum
     * @return
     */
    public ResultJson deleteAccountBalanceFlow(String tradenum){
        AccountBalanceFlow accountBalanceFlow = new AccountBalanceFlow();
        accountBalanceFlow.setTradenum(tradenum);
        List<AccountBalanceFlow> accountBalanceFlowList = accountBalanceFlowDao.getList(accountBalanceFlow);
        if (accountBalanceFlowList == null || accountBalanceFlowList.size() == 0)
            return new ResultJson();
        else {
            for (AccountBalanceFlow balanceFlow : accountBalanceFlowList)
                accountBalanceFlowDao.delete(balanceFlow);
            return new ResultJson();
        }
    }
}
